import Sprite from '../base/sprite'
import Bouns from '../npc/bonus'
import { screenWidth, screenHeight, ROLE_STATE, SPCIES_TYPE } from '../../constant/index'
import { getCanvas } from '../../../ctx'
import { getCloudUrl } from '../../utils'

// 玩家相关常量设置
const CHARACTER_IMG_SRC = getCloudUrl('images/nmdgr.png')
const CHARACTER_WIDTH = 150 * 1.5
const CHARACTER_HEIGHT = 150 * 1.5

export default class Character extends Sprite {
  constructor() {
    super(CHARACTER_IMG_SRC, CHARACTER_WIDTH, CHARACTER_HEIGHT)
    this.init()
  }
  createdBonusPoints (v, type) {
    const bonus = new Bouns()
    bonus.darwingType = type
    bonus.value = v
    bonus.x = this.x + bonus.left
    bonus.x = this.y + bonus.top
    this.appendChild(bonus)
  }
  /**
   * 初始属性
   */
  init () {
    // 玩家默认处于屏幕底部居中位置
    this.x = screenWidth / 2 - this.width / 2
    this.y = screenHeight - this.height - 30
    this.currentState = ROLE_STATE.INIT.type
    this.img.src = ROLE_STATE[this.currentState].src
    this.ROLE_STATE = ROLE_STATE
    Object.keys(ROLE_STATE).forEach(key => {
      const name = `${key}-img`
      this[name] = getCanvas().createImage()
      this[name].src = ROLE_STATE[key].src
    })
  }
  /**
   * 设置状态
   */
  setStateType(type = ROLE_STATE.INIT.type) {
    if (this.currentState === type) return
    this.currentState = type
  }
  /**
 * 更新坐标
 */
  update() {
    this.children.forEach((child) => child.update(this))
  }
  
  /**
   * 将精灵图绘制在canvas上
   */
  render(ctx) {
    if (!this.visible) return
    const name = `${this.currentState}-img`
    if (this[name]) {
      ctx.drawImage(
        this[name],
        this.x,
        this.y,
        this.width,
        this.height
      )
    }
    this.children.forEach(child => child.render(ctx))
  }
}
